package com.mandala.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.mandala.LDAPConnectService;
import com.mandala.entry.Group;
import com.mandala.entry.ResponseWrapper;
import com.mandala.entry.User;
import com.mandala.service.JsonResult;
import com.mandala.service.OdmUserRepo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/group")
@Api("群组查询接口")
public class GroupController {
	@Autowired
	private OdmUserRepo odmUserRepo;
	@Autowired
	private JsonResult jsonResult;
	@Autowired
	private LDAPConnectService connectService;
	
	static Map<Long, Group> groups = Collections.synchronizedMap(new HashMap<Long, Group>());
	
    @ApiOperation(value = "根据群组CN", notes = "群组查询")
    @ApiImplicitParam(name = "cn", value = "群组ID", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = "/getGroupByCn/{cn}" , method = RequestMethod.GET)
    public  ResponseWrapper getGroupByCn(@PathVariable("cn") String cn) {
    	if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	ResponseWrapper response = new ResponseWrapper();
    	if(cn.equals("null") || cn ==null){
    		return jsonResult.markParamError();
    	}
    	Group group = new Group();
    	group = odmUserRepo.findGroupByCn(cn);
		if(group != null){
			response = jsonResult.markSuccess(group);
        }else{
        	response = jsonResult.markSuccessButNoData();
        }
        return response;
    }
    
    @ApiOperation(value = "根据群组CN", notes = "群组人员查询")
    @ApiImplicitParam(name = "cn", value = "群组ID", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = "/getGroupUserByCn/{cn}" , method = RequestMethod.GET)
    public  ResponseWrapper getGroupUserByCn(@PathVariable("cn") String cn) {
    	if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	ResponseWrapper response = new ResponseWrapper();
    	if(cn.equals("null") || cn ==null){
    		return jsonResult.markParamError();
    	}
    	Group group = new Group();
    	group = odmUserRepo.findGroupByCn(cn);
    	List<User> listUser = new ArrayList<>();
		if(group != null){
			List<String> listMember = group.getMember();
			if(listMember.size()>0){
				for(int i=0;i<listMember.size();i++){
					User user = new User();
					// cn=defaultUser,ou=user,dc=xiehe,dc=com 这是获取到的成员信息
					String memberCn = (listMember.get(i).split(",")[0]).substring(3);//获取成员的id
					user = odmUserRepo.findUserByCn(memberCn);
					if(user != null){
						listUser.add(user);
					}
				}
				
			}
			response = jsonResult.markSuccess(listUser);
        }else{
        	response = jsonResult.markSuccessButNoData();
        }
        return response;
    }
    
    /*@ApiOperation(value="获取所有群组列表", notes="获取所有群组列表")
    @RequestMapping(value={""}, method=RequestMethod.GET)
    public List<Group> getGroupList() {
        //List<Group> r = new ArrayList<Group>(groups.values());
    	List<Group> r = odmUserRepo.findAllGroup();
          return r;
     }*/
    
    @ApiOperation(value = "根据用户id", notes = "群组信息查询")
    @ApiImplicitParam(name = "member", value = "用户ID", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = "/getGroupByUserId/{member}" , method = RequestMethod.GET)
    public  List<Group> getGroupByUserId(@PathVariable("member") String member) {
    	List<Group> group = odmUserRepo.findAllGroupByUserId(member);
        return group;
    }
}